<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="创建人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库" prop="stockName">
        <el-input
          v-model="queryParams.stockName"
          placeholder="请输入仓库名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="timeArr">
        <el-date-picker
          v-model="queryParams.timeArr"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          @change="timeArrChange"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['recovery:check-log:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="checkLogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" width="80" />
      <el-table-column label="仓库" align="center" prop="stockName" width="100" />
      <el-table-column label="未盘" align="center" prop="goodsIds" show-overflow-tooltip>
        <template v-slot="scope">
          <div>
            <el-tag style="margin: 3px" v-for="g in scope.row.goodsList">{{g}}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="未盘数量" align="center" width="100">
        <template v-slot="scope">
          {{scope.row.goodsList.length}}
        </template>
      </el-table-column>
      <el-table-column label="已盘数量" align="center" prop="checkNo" width="100" />
      <el-table-column label="异常数量" align="center" prop="abnormalNo" width="100" />
      <el-table-column label="创建人" align="center" prop="createBy" width="100" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
      <el-table-column label="操作" align="center" width="180">
        <template v-slot="scope">
          <el-button type="text" size="mini" @click="handleAdd(scope.row)">处理记录</el-button>
          <el-button
            type="text"
            icon="el-icon-download"
            size="mini"
            @click="handleExportRow(scope.row)"
            v-hasPermi="['recovery:check-log:export']"
          >未盘导出</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :visible.sync="open" title="处理记录" width="60%" append-to-body>
      <el-form label-width="100px" label-position="left">
        <el-form-item label="商品编码">
          <el-select v-model="form.goodsId" style="width: 100%" filterable>
            <el-option v-for="item in checkLogGoodsList(logRow.goodsIds)" :value="item" :label="item" />
          </el-select>
        </el-form-item>
        <el-form-item label="处理描述">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入" />
        </el-form-item>
      </el-form>

      <el-button type="primary" @click="submitForm">添加</el-button>

      <div style="margin-top: 10px">
        <el-table :data="handleCheckList" border>
          <el-table-column align="center" label="商品编码" prop="goodsId" width="180" />
          <el-table-column align="center" label="描述" prop="remark" />
          <el-table-column align="center" label="创建人" prop="createBy" />
          <el-table-column align="center" label="创建时间" prop="createTime" />
        </el-table>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCheckLog, exportCheckLog, exportRow, listHandleCheckLogList, addHandleCheckLogList } from "@/api/recovery/check-log";

export default {
  name: "Check-log",
  components: {
  },
  data() {
    return {
      handleCheckList: [],
      logRow: {
        goodsIds: "",
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 盘库日志表格数据
      checkLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsIds: null,
        checkNo: null,
        abnormalNo: null,
        createBy: null,
        stockName: null,
        timeArr: [],
        params: {}
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleExportRow(row) {
      this.$confirm("确定导出当前行的未盘数据吗？", "提示", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type:'warning'
      }).then(() => {
        return exportRow({id: row.id});
      }).then(res => {
        this.download(res.msg);
      })
    },
    timeArrChange(e) {
      if(e) {
        this.$set(this.queryParams.params, "startTime", e[0]);
        this.$set(this.queryParams.params, "endTime", e[1]);
      }else {
        this.$delete(this.queryParams.params, "startTime");
        this.$delete(this.queryParams.params, "endTime");
      }
    },
    /** 查询盘库日志列表 */
    getList() {
      this.loading = true;
      listCheckLog(this.queryParams).then(response => {
        this.checkLogList = response.rows;
        this.checkLogList.forEach(c => {
          c["goodsList"] = c.goodsIds.split(",");
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goodsId: null,
        checkLogId: null,
        remark: null
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.timeArrChange(null);
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.open = true;
      this.logRow = row;
      this.getHandleCheckLogList();
    },
    /** 提交按钮 */
    submitForm() {
      if(!this.form.goodsId){
        this.msgWarning("请选择商品编码");
        return
      }
      if(!this.form.remark) {
        this.msgWarning("描述不能为空");
        return
      }

      this.form.checkLogId = this.logRow.id
      addHandleCheckLogList(this.form).then(res => {
        this.msgSuccess("操作成功");
        this.reset();
        this.getHandleCheckLogList();
      })
    },
    getHandleCheckLogList() {
      listHandleCheckLogList({checkLogId: this.logRow.id}).then(res => {
        this.handleCheckList = res.data;
      })
    },
    checkLogGoodsList(val) {
      let arr = [];
      let goods = val.split(",");
      goods.forEach(item => {
        let bool = true;
        this.handleCheckList.forEach(it => {
          if(it.goodsId === item)
            bool = false
        })

        if(bool)
          arr.push(item)
      })

      return arr;
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有盘库日志数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCheckLog(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
